import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_widget.dart';
import 'package:pornhub_app/components/no_more/no_data_scroll_view.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/model/mine/publications_posts_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/color.dart';

import '../controllers/posts_page_controller.dart';
import 'item_posts_view.dart';

class PostsPage extends StatefulWidget {
  const PostsPage({super.key});

  @override
  State<StatefulWidget> createState() => _PostsPageState();
}

class _PostsPageState extends State<PostsPage>
    with SingleTickerProviderStateMixin {
  final controller = Get.find<PostsPageController>();

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      child: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return [
            SliverAppBar(
              pinned: true,
              title: TabBar(
                controller: controller.tabController,
                isScrollable: false,
                labelColor: COLOR.color_D8201D,
                unselectedLabelColor: COLOR.color_333333,
                indicator: const BoxDecoration(),
                dividerHeight: 0,
                labelStyle:
                    TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w600),
                tabs: controller.tabs.map((entry) {
                  return Container(
                    height: 23.h,
                    padding: EdgeInsets.symmetric(horizontal: 14.w),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(11.5.w),
                    ),
                    alignment: Alignment.center,
                    child: TextView(
                      text: entry.text ?? "",
                      // color: COLOR.white,
                    ),
                  );
                }).toList(),
              ),
              elevation: 0,
              leadingWidth: 0,
              backgroundColor: Colors.white,
              shadowColor: const Color.fromRGBO(0, 0, 0, 0),
              surfaceTintColor: Colors.white,
              leading: const SizedBox.shrink(),
            ),
            // SliverToBoxAdapter(
            //   child: Visibility(
            //     visible: true,
            //     child: localDraftsView(),
            //   ), // Spacer
            // ),
          ];
        },
        body: TabBarView(
          controller: controller.tabController,
          children: [
            Obx(
              () => _buildTabContent(controller.publications.value, 1),
            ),
            Obx(
              () => _buildTabContent(controller.underReviews.value, 2),
            ),
            Obx(
              () => _buildTabContent(controller.rejecteds.value, 3),
            ),
          ],
        ),
      ),
    );
  }

  localDraftsView() {
    return Container(
      height: 35.h,
      decoration: BoxDecoration(
        color: COLOR.color_FFE5E5,
        borderRadius: BorderRadius.circular(6.w),
      ),
      padding: EdgeInsets.symmetric(horizontal: 10.w),
      child: Row(
        children: [
          Image.asset(
            'assets/mine/icon_mine_local_drafts.png',
            width: 20.w,
            height: 20.w,
          ),
          SizedBox(width: 10.w),
          TextView(
            text: "本地草稿",
            color: COLOR.color_FF4340,
            fontSize: 12.sp,
            fontWeight: FontWeight.w500,
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: GestureDetector(
                onTap: () {
                  Get.toNamed(Routes.my_publications_local_drafts);
                },
                child: TextView(
                  text: "有2篇帖子待发布>>",
                  color: COLOR.color_FF4340,
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _buildTabContent(
      List<PublicationsPostsModel> postsList, int fromType) {
    return BaseRefreshWidget(
      controller,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 14.w),
        child: postsList.isNotEmpty
            ? ListView.builder(
                itemCount: postsList.length,
                itemBuilder: (BuildContext context, int index) {
                  return ItemPostsView(
                    fromType: fromType,
                    model: postsList[index],
                  );
                },
              )
            : const NoDataScrollView(),
      ),
    );
  }

  @override
  void dispose() {
    // controller.tabController.dispose();
    super.dispose();
  }
}
